package grape.composite.rest.railway.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import grape.common.exception.runtime.RBaseException;
import grape.composite.rest.railway.form.RailwayStationListForm;
import grape.composite.service.railway.query.RailwayStationListQuery;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.validation.Valid;
import grape.composite.rest.railway.form.RailwayStationCreateForm;
import grape.composite.rest.railway.form.RailwayStationUpdateForm;
import grape.composite.rest.railway.form.RailwayStationListPageForm;
import grape.composite.rest.railway.vo.RailwayStationVo;
import grape.composite.rest.railway.mapper.RailwayStationWebMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import grape.common.rest.mvc.BaseController;
import grape.composite.service.railway.po.RailwayStation;
import grape.composite.service.railway.api.IRailwayStationService;
import java.util.List;
/**
 * <p>
 * 铁路站点 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-09-10
 */
@RestController
@RequestMapping("/composite/railwaystation")
@Api(tags = "铁路站点相关接口")
public class RailwayStationController extends BaseController<RailwayStationVo, RailwayStation> {

    @Autowired
    private RailwayStationWebMapper currentWebMapper;
    @Autowired
    private IRailwayStationService currentService;



     @ApiOperation("添加铁路站点")
     @PreAuthorize("hasAuthority('railwaystation:single:create')")
     @PostMapping
     @ResponseStatus(HttpStatus.CREATED)
     public RailwayStationVo create(@RequestBody @Valid RailwayStationCreateForm cf) {
         // code 唯一检查
         if (currentService.existCode(cf.getCode())) {
             throw new RBaseException("编码已存在");
         }
         RailwayStation po = currentWebMapper.formToPo(cf);
         return super.create(po);
     }

     @ApiOperation("根据ID查询铁路站点")
     @PreAuthorize("hasAuthority('railwaystation:single:queryById')")
     @GetMapping("/{id}")
     @ResponseStatus(HttpStatus.OK)
     public RailwayStationVo queryById(@PathVariable String id) {
         return super.queryById(id);
     }

     @ApiOperation("删除铁路站点")
     @PreAuthorize("hasAuthority('railwaystation:single:deleteById')")
     @DeleteMapping("/{id}")
     @ResponseStatus(HttpStatus.NO_CONTENT)
     public boolean deleteById(@PathVariable String id) {
         return super.deleteById(id);
     }

     @ApiOperation("更新铁路站点")
     @PreAuthorize("hasAuthority('railwaystation:single:updateById')")
     @PutMapping("/{id}")
     @ResponseStatus(HttpStatus.CREATED)
     public RailwayStationVo update(@PathVariable String id,@RequestBody @Valid RailwayStationUpdateForm uf) {
         RailwayStation po = currentWebMapper.formToPo(uf);
         po.setId(id);
         return super.update(po);
     }

    @ApiOperation("不分页查询铁路站点")
    @PreAuthorize("hasAuthority('railwaystation:single:list')")
    @GetMapping("/list")
    @ResponseStatus(HttpStatus.OK)
    public List<RailwayStationVo> list(RailwayStationListForm listForm) {
        RailwayStation po = currentWebMapper.formToPo(listForm);
        RailwayStationListQuery railwayStationListQuery = new RailwayStationListQuery();
        railwayStationListQuery.setPoQuery(po);
        return super.list(railwayStationListQuery);
    }

    @ApiOperation("分页查询铁路站点")
    @PreAuthorize("hasAuthority('railwaystation:single:listPage')")
    @GetMapping("/listPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<RailwayStationVo> listPage(RailwayStationListPageForm listPageForm) {
         RailwayStation po = currentWebMapper.formToPo(listPageForm);
        RailwayStationListQuery railwayStationListQuery = new RailwayStationListQuery();
        railwayStationListQuery.setPoQuery(po);
         return super.listPage(railwayStationListQuery,listPageForm);
     }

}
